<?php

namespace app\model;

use app\utils\Md5Service;
use \think\facade\Db;
use think\facade\Log;
use think\facade\Request;

class WmsCustomerModel
{


    public static function  list($param): array
    {


        $where[] = [
            'partner_id', '=', $param['partner_id']
        ];
        $where[] = ['status', '<', 3];
        if (!empty($param['name'])) {
            $where[] = ['name', 'like', '%' . $param['name'] . '%'];
        }
        if (!empty($param['contract_no'])) {
            $where[] = ['contract_no', '=', $param['contract_no']];
        }

        $warehouseType = Db::table('xl_customer')
            ->where([$where])->limit(($param['page'] - 1) * $param['limit'], $param['limit'])
            ->select()->toArray();

        $count = Db::table('xl_customer')->where([$where])->count();
        return ['list' => $warehouseType, 'count' => $count];
    }

    public static function line_data($param): array
    {
        $where[] = [
            'partner_id', '=', $param['partner_id']
        ];
        $where[] = ['status', '<', 3];

        $warehouseType = Db::table('xl_warehouse')
            ->where([$where])
            ->field('id,name,longitude,dimension')
            ->select()->toArray();

        return $warehouseType;
    }

    /**
     * 获取所有仓库类型
     * @return void
     */
    public static function GetAllWarehouseType(): array
    {
        $warehouseType = Db::table('xl_warehouse_type')->where("status", "=", 1)
            ->select()->toArray();
        $redisLink = linkRedis(11);
        $warehouseTypeData = $redisLink->get('warehouse_type');
        if ($warehouseTypeData) {
            return json_decode($warehouseTypeData, true);
        } else {
            $redisLink->set('warehouse_type', json_encode($warehouseType));
            return $warehouseType;
        }

    }

    /**
     * 获取所有业务类型
     * @return void
     */
    public static function GetAllBussineType(): array
    {

        $businessType = Db::table('xl_business_type')->where("status", "=", 1)
            ->select()->toArray();
        $redisLink = linkRedis(11);
        $businessTypeData = $redisLink->get('business_type');
        if ($businessTypeData) {
            return json_decode($businessTypeData, true);
        } else {
            $redisLink->set('business_type', json_encode($businessType));
            return $businessType;
        }


    }

    /**
     * 新增所有仓库
     * @return void
     */
    public static function add($data)
    {
        return Db::table('xl_customer')->insert($data);

    }

    /**
     * 新增所有仓库
     * @return int
     */
    public static function edit($data, $id)
    {

        return Db::table('xl_customer')->where('id', '=', $id)->update($data);

    }


    /**
     * 删除仓库
     * @return void
     */
    public static function del($id)
    {
        return Db::table('xl_customer')->where('id', '=', $id)->update(['status' => 3, 'update_time' => getNowDateTime()]);

    }


    public static function info($id)
    {
        return Db::table('xl_customer')->where('id', '=', $id)->field('id,name,longitude,dimension')->find();
    }


}
